package koction.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import koction.model.consumer.ConsumerService;
import koction.model.consumer.ConsumerVO;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class ConsumerController {
	@Resource(name="consumerServiceImpl")
	private ConsumerService consumerService;
	
	@RequestMapping("/consumer.do")
	public String asd(){
		return "index";
	}
	
	@RequestMapping("/consumer_login.do")
	public String loginConsumerPage(){
		return "loginConsumer.consumer";
	}
	@RequestMapping("/consumer_register.do")
	public String registerConsumerPage(){
		return "registerConsumer.consumer";
	}
	@RequestMapping("/consumer_update.do")
	public String updateConsumerPage(){
		return "updateConsumer.consumer";
	}
	
	@RequestMapping("/loginconsumer.do")
	public ModelAndView loginConsumer(HttpSession session, ConsumerVO cvo){
		ConsumerVO cvo1=consumerService.loginConsumer(cvo);
		String path="";
		if(cvo1==null){
			path="loginConsumerFail.consumer";
		} else {
			session.setAttribute("cvo", cvo1);
			path="home";
		}
		return new ModelAndView(path);
	}
	
	@RequestMapping("/logoutconsumer.do")
	public ModelAndView logoutConsumer(HttpSession session){
		session.invalidate();
		return new ModelAndView("home");
	}
	
	@RequestMapping("/idcheckconsumer.do")
	@ResponseBody
	public Map consumerIdCheck(String id){
		ConsumerVO cvo=consumerService.consumerIdCheck(id);
		Map map=new HashMap();
		map.put("cvo", cvo);
		return map;
	}
	
	@RequestMapping("/registerconsumer.do")
	public ModelAndView registerConsumer(ConsumerVO cvo){
		int result=consumerService.registerConsumer(cvo);
		return new ModelAndView("registerConsumerOk.consumer");
	}
	
	@RequestMapping("/updateconsumer.do")
	public ModelAndView updateConsumer(HttpSession session, ConsumerVO cvo){
		consumerService.updateConsumer(cvo);
		session.setAttribute("cvo", cvo);
		return new ModelAndView("updateConsumerOk.consumer");
	}
	
	@RequestMapping("/deleteconsumer.do")
	public ModelAndView deleteConsumer(HttpSession session, ConsumerVO cvo){
		ConsumerVO cvo1=(ConsumerVO) session.getAttribute("cvo");
		consumerService.deleteConsumer(cvo1);
		session.invalidate();
		return new ModelAndView("home");
	}
	
}
